package com.crm.controller;

import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.Result;
import com.crm.pojo.dto.DeptDto;
import com.crm.pojo.vo.DeptVo;
import com.crm.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.List;

@RestController
@RequestMapping("/system/dept")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
    // 部门分页查询
    @GetMapping("/list")
    public Result<List<DeptVo>> list(DeptDto deptDto){
        return Result.success(deptService.list(deptDto));
    }
    //部门新增
    @PostMapping
    public Result add(@RequestBody  DeptDto deptDto){
        try {
            deptService.add(deptDto);
            return Result.success();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    //部门查询（id）
    @GetMapping("/{id}")
    public Result<DeptVo> get(@PathVariable Long id){
        return Result.success(deptService.getById(id));
    }
    //部门删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        try {
            deptService.deleteById(id);
        } catch (Exception e) {
            log.info(e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }
    //部门修改
    @PutMapping
    public Result update(@RequestBody DeptDto deptDto){
        try {
            deptService.update(deptDto);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    //不知道干嘛的
    @GetMapping("/list/exclude/{id}")
    public Result<List> exclude(@PathVariable Long id){
        return Result.success(deptService.exclude(id));
    }

    //部门下拉列表
    @GetMapping("/treeselect")
    public Result deptTreeSelect(){
        return Result.success(deptService.deptTreeSelect(0L));
    }
}
